package com.luxiu.extensionlib.cardView

import android.animation.ValueAnimator
import android.content.Context
import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.view.animation.AccelerateDecelerateInterpolator
import android.widget.RelativeLayout
import androidx.core.content.ContextCompat
import com.luxiu.extensionlib.R


class CardRelativeLayout(
    context: Context?, attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = 0,
) : RelativeLayout(context, attrs, defStyleAttr, defStyleRes) {

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attrs: AttributeSet) : this(context, attrs, 0)

    init {
        val ta = context!!.obtainStyledAttributes(attrs, R.styleable.CardRelativeLayout, defStyleAttr, defStyleRes)
        try {
            // 保存属性数据以便后续使用，可用于恢复视图状态等操作
            saveAttributeDataForStyleable(context, R.styleable.CardRelativeLayout, attrs, ta , defStyleAttr, defStyleRes)

            // 获取颜色资源 ID，如果未指定则使用默认的白色
            val colorRes = ta.getResourceId(R.styleable.CardRelativeLayout_mColorRes, R.color.white)
            // 获取卡片圆角半径，如果未指定则默认为 0f
            val cardCornerRadius = ta.getDimension(R.styleable.CardRelativeLayout_mCardCornerRadius, 0f)
            // 获取卡片的阴影高度，如果未指定则默认为 0f
            val elevationFloat = ta.getDimension(R.styleable.CardRelativeLayout_mElevation, 0f)

            // 创建一个 GradientDrawable 作为背景
            val drawable = GradientDrawable().apply {
                shape = GradientDrawable.RECTANGLE
                setColor(ContextCompat.getColor(context, colorRes))
                cornerRadius = cardCornerRadius
            }
            background = drawable
            clipToOutline = true
            elevation = elevationFloat
        } finally {
            ta.recycle()
        }
    }
}